﻿Imports System.Collections.Specialized
Imports System.Text

Public Class SiteForm

#Region "创建歌曲数据源"
    Private Function CreateDataSourceSong() As DataTable

        Dim dt As New DataTable
        Dim dc As DataColumn
        dc = dt.Columns.Add("分类1")
        dc.DefaultValue = ""

        dc = dt.Columns.Add("分类2")
        dc.DefaultValue = ""

        dc = dt.Columns.Add("标题")
        dc.DefaultValue = ""

        dc = dt.Columns.Add("网址")
        dc.DefaultValue = ""

        Dim sb As New StringBuilder
        sb.AppendLine(" Select * ")
        sb.AppendLine(" ,XscBugType.ID WebSiteTypeId ")
        sb.AppendLine(" ,XscBugType.ParentID WebSiteTypeParentID ")
        sb.AppendLine(" ,XscBugType.Name ")
        sb.AppendLine("   From XscWebSite")
        sb.AppendLine("   LEFT JOIN XscBugType ")
        sb.AppendLine("     ON XscWebSite.type=XscBugType.id ")
        Dim dt1 As DataTable = ct.FillTable(sb.ToString())

        sb.Length = 0
        sb.AppendLine(" Select * ")
        sb.AppendLine(" From  XscBugType")
        sb.AppendLine(" Where Type = 4 ")

        Dim dtType As DataTable = ct.FillTable(sb.ToString())

        If dtType.Rows.Count > 0 Then
            Dim drParents() As DataRow
            Dim drParents1() As DataRow
            Dim drParentDetail() As DataRow

            drParents = dtType.Select(" ParentID is Null ")
            If drParents.Length > 0 Then
                For jdex As Integer = 0 To drParents.Length - 1
                    drParents1 = dtType.Select(" ParentID = '" + CStr(drParents(jdex)("id")) + "'")
                    If drParents1.Length > 0 Then
                        For kdex As Integer = 0 To drParents1.Length - 1
                            drParentDetail = dt1.Select(" WebSiteTypeId = '" + CStr(drParents1(kdex)("id")) + "'")
                            If drParentDetail.Length > 0 Then
                                For mdex As Integer = 0 To drParentDetail.Length - 1
                                    Dim dr As DataRow = dt.NewRow
                                    dr("分类1") = CStr(drParents(jdex)("name"))
                                    dr("分类2") = CStr(drParents1(kdex)("name"))
                                    dr("标题") = CStr(drParentDetail(mdex)("TITLE"))
                                    dr("网址") = CStr(drParentDetail(mdex)("WEBSITEURL"))
                                    dt.Rows.Add(dr)
                                Next
                            End If

                        Next
                    End If

                Next
            End If

        End If
        Return dt
    End Function
#End Region

    Private Sub PrintProcess(ByVal content As String)
        lbl1000.Text = content
        System.Windows.Forms.Application.DoEvents()
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Try

            Dim englishFolder As String = System.IO.Path.Combine(GetCurrentDirectory, "Report")
            If Not System.IO.Directory.Exists(englishFolder) Then
                System.IO.Directory.CreateDirectory(englishFolder)
            End If

            Dim englishPath As String = System.IO.Path.Combine(englishFolder, "Site.xls")
            Dim bCreate As Boolean = True
            If System.IO.File.Exists(englishPath) Then
                System.IO.File.Delete(englishPath)
            End If

            Dim dt As System.Data.DataTable = CreateDataSourceSong()
            If dt.Rows.Count = 0 Then
                ShowMessageBox("没有数据")
                Exit Sub
            End If

            For jndex As Integer = 0 To dt.Rows.Count - 2
                For kdex As Integer = jndex + 1 To dt.Rows.Count - 1
                    If dt.Rows(jndex)("分类1") = dt.Rows(kdex)("分类1") _
                    AndAlso dt.Rows(jndex)("分类2") = dt.Rows(kdex)("分类2") Then
                        dt.Rows(kdex)("分类2") = ""
                    End If
                Next
            Next

            For jndex As Integer = 0 To dt.Rows.Count - 2
                For kdex As Integer = jndex + 1 To dt.Rows.Count - 1
                    If dt.Rows(jndex)("分类1") = dt.Rows(kdex)("分类1") Then
                        dt.Rows(kdex)("分类1") = ""
                    End If
                Next
            Next

            Dim englishCreate As New Site(englishPath)
            englishCreate.报表记录条数字 = Me.报表记录条数字
            englishCreate.报表字体 = Me.报表字体

            AddHandler englishCreate.PrintProcess, AddressOf PrintProcess

            englishCreate.DoOutData(dt)
            RemoveHandler englishCreate.PrintProcess, AddressOf PrintProcess

            '打开文件
            Using pro As New Process
                pro.StartInfo.FileName = englishPath
                pro.Start()
            End Using

        Catch ex As Exception
            ShowMessageBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

#Region "报表必须使用"
    Private _报表字体 As String = "宋体"
    Public Overrides Property 报表字体() As String
        Get
            Return _报表字体
        End Get
        Set(ByVal value As String)
            _报表字体 = value
        End Set
    End Property

    Private _报表记录条数字 As Integer = 55
    Public Overrides Property 报表记录条数字() As Integer
        Get
            Return _报表记录条数字
        End Get
        Set(ByVal value As Integer)
            _报表记录条数字 = value
        End Set
    End Property
#End Region

End Class
